package com.lc.soft.service.impl;

import com.lc.soft.dao.IDeviceDao;
import com.lc.soft.entity.CommonParameter;
import com.lc.soft.entity.DeviceEntity;
import com.lc.soft.service.IDeviceService;
import com.lc.soft.utils.ResponseResult;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Log4j2
public class DeviceService implements IDeviceService {

    private IDeviceDao deviceDao;

    @Autowired
    public DeviceService(IDeviceDao deviceDao) {
        this.deviceDao = deviceDao;
    }

    @Override
    public ResponseResult create(DeviceEntity deviceEntity) {
        try {
            int createResult = deviceDao.create(deviceEntity);
            if (createResult == 0) {
                return ResponseResult.OPERATION_FAILED;
            }
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Override
    public ResponseResult update(DeviceEntity deviceEntity) {
        try {
            int updateResult = deviceDao.update(deviceEntity);
            if (updateResult == 0) {
                return ResponseResult.OPERATION_FAILED;
            }
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Override
    public ResponseResult updateState(DeviceEntity deviceEntity) {
        try {
            int updateResult = deviceDao.updateState(deviceEntity);
            if (updateResult == 0) {
                return ResponseResult.OPERATION_FAILED;
            }
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Override
    public ResponseResult findDeviceListByCondition(CommonParameter commonParameter) {
        try {
            List<DeviceEntity> deviceEntityList = deviceDao.findDeviceListByCondition(commonParameter);
            if (deviceEntityList == null || deviceEntityList.size() == 0) {
                return ResponseResult.DATA_NOT_AVAILABLE;
            }
            return ResponseResult.success(deviceEntityList);
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Override
    public ResponseResult findPagedDeviceListByCondition(CommonParameter commonParameter) {
        try {
            List<DeviceEntity> deviceEntityList = deviceDao.findPagedDeviceListByCondition(commonParameter);
            if (deviceEntityList == null || deviceEntityList.size() == 0) {
                return ResponseResult.DATA_NOT_AVAILABLE;
            }
            int amount = deviceDao.findAmountByCondition(commonParameter);
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("list", deviceEntityList);
            resultMap.put("amount", amount);
            return ResponseResult.success(resultMap);
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }
}
